var http = require('http')
var fs = require('fs')

var server = http.createServer()

var wwwDir = '/Users/china/Documents/lujianying/nodejs/www'

server.on('request', function (req, res) {
   var url = req.url
    // / index.html
    // /a.txt   wwwDir + /a.txt
    // /apple/login.html     wwwDir + /apple/login.html  
     
    if (url === '/') {
        fs.readFile(wwwDir + '/index.html', function (err, data) {
            if (err) {
                return res.end('404 Not Found')
            }
            res.end(data)
        })
    } else if (url === '/a.txt') {
        fs.readFile(wwwDir + '/a.txt', function (err, data) {
            if (err) {
                return res.end('404 Not Found')
            }
            res.setHeader('Content-Type', 'text/plain; charset=utf-8')
            res.end(data)
        })
    } else if (url === '/apple/login.html') {
        fs.readFile(wwwDir + '/apple/login.html', function (err, data) {
            if (err) {
                return res.end('404 Not Found')
            }
            res.end(data)
        })
    }
})

server.listen(8000, function () {
    console.log('server is running...')
})